package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.ruoyi.system.domain.CompanyJob;
import com.ruoyi.system.domain.CompanyJob89624;
import com.ruoyi.system.domain.SmaiSocialinsurance;
import com.ruoyi.system.domain.SmaiSocialinsurance87203;
import com.ruoyi.system.mapper.CompanyJob89624Mapper;
import com.ruoyi.system.mapper.SmaiSocialinsurance87203Mapper;
import com.ruoyi.system.service.AddTestService;
import com.ruoyi.system.service.SelectTestService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;


@EnableScheduling
@RequiredArgsConstructor
@Service
@Slf4j
public class AddTestServiceImpl implements AddTestService {

    private final static Logger logger = LoggerFactory.getLogger(AddTestServiceImpl.class);

    private final CompanyJob89624Mapper companyJob89624Mapper;

    private final SelectTestService selectTestService;

    private final SmaiSocialinsurance87203Mapper smaiSocialinsurance87203Mapper;

    @Override
    public void addaddJob() {
        List<CompanyJob> companyJobs = selectTestService.selectJobCompanyJob();
        if (companyJobs != null && companyJobs.size() > 0) {
            for (CompanyJob companyJob : companyJobs) {
                CompanyJob89624 companyJob89624 = companyJob89624Mapper.selectVoById(companyJob.getId());
                if (companyJob89624 == null) {
                    // 新增
                    CompanyJob89624 add = new CompanyJob89624();
                    BeanUtil.copyProperties(companyJob,add);
                    add.setOp("insert");
                    companyJob89624Mapper.insert(add);
                } else {
                    // 修改
                    CompanyJob89624 updata = new CompanyJob89624();
                    BeanUtil.copyProperties(companyJob,updata);
                    updata.setId(companyJob89624.getId());
                    updata.setOp("update");
                    companyJob89624Mapper.updateById(updata);
                }
            }
        }
    }

    @Override
    public void addaddSb() {
        List<SmaiSocialinsurance> smaiSocialinsurances = selectTestService.selectSmai();
        log.info("本地数据查询结果集{}",smaiSocialinsurances);
        if (smaiSocialinsurances != null && smaiSocialinsurances.size() > 0) {
//            /**
//             * 临时应急数据推送（单条）
//             */
//            SmaiSocialinsurance87203 add = new SmaiSocialinsurance87203();
//            BeanUtil.copyProperties(smaiSocialinsurances.get(0),add);
//            add.setOp("update");
//            int insert = smaiSocialinsurance87203Mapper.insert(add);
//            log.info("应急新增受影响行数{}",Convert.toStr(insert));
//            log.info("应急新增成功{}",add);

            Integer localDataNumber = smaiSocialinsurances.size();
            Integer dmDataNumber = 0;
            for (SmaiSocialinsurance smaiSocialinsurance : smaiSocialinsurances) {
                SmaiSocialinsurance87203 smaiSocialinsurance87203 = smaiSocialinsurance87203Mapper.queryById(smaiSocialinsurance.getId());
                if (smaiSocialinsurance87203 == null) {
                    SmaiSocialinsurance87203 add = new SmaiSocialinsurance87203();
                    BeanUtil.copyProperties(smaiSocialinsurance,add);
                    add.setOp("update");
                    int insert = smaiSocialinsurance87203Mapper.insert(add);
                    log.info("正常新增受影响行数{}",Convert.toStr(insert));
                    log.info("正常新增成功{}",insert);
                } else {
                    log.info("id查询成功{}",smaiSocialinsurance87203);
                    dmDataNumber += 1;
                }
            }
            if (localDataNumber.equals(dmDataNumber)) {
                log.info("指定一条数据推送");
                SmaiSocialinsurance87203 add = new SmaiSocialinsurance87203();
                BeanUtil.copyProperties(smaiSocialinsurances.get(0),add);
                add.setOp("update");
                int insert = smaiSocialinsurance87203Mapper.insert(add);
                log.info("指定新增受影响行数{}",Convert.toStr(insert));
                log.info("指定新增成功{}",add);
            }
        }
    }

    // 5的倍数执行
//    @Scheduled(cron = "0 0/5 * * * ?")
    // 每天12点推送一次
    @Scheduled(cron = "0 0 12 * * ?")
    public void text() {
        try {
            System.out.println("定时执行开始...... ");
            this.addaddSb();
            log.info("定时执行结束。。。。。。 ");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
